package com.maxd.controller.selfWXPay;

import com.maxd.adapter.IgnoreSecurity;
import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.service.selfWXPay.SelfWXService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.Map;

@Api(value="自营商城微信支付",tags={"自营商城微信支付"})
@RestController
@RequestMapping("/pay")
public class SelfWXPayController {
    private final Logger log = LoggerFactory.getLogger(SelfWXPayController.class);
    @Autowired
    private SelfWXService service;

    @IgnoreSecurity
    @ApiOperation("app支付")
    @PostMapping("/wxPayApp")
    public Map wxPayApp(Long ordersId, Long userId) throws Exception {
        return service.doUnifiedOrder(ordersId,1, userId);
    }

    @IgnoreSecurity
    @ApiOperation("公众号JSAPI支付")
    @PostMapping("/wxPayWeb")
    public Map wxPayWeb(Long ordersId, Long userId) throws Exception {
        return service.doUnifiedOrder(ordersId,2, userId);
    }

    @IgnoreSecurity
    @ApiOperation("小程序支付")
    @PostMapping("/wxPayMp")
    public Map wxPayMp(Long ordersId, Long userId) throws Exception {
        return service.doUnifiedOrder(ordersId,3, userId);
    }

    @IgnoreSecurity
    @ApiOperation("app支付")
    @PostMapping("/wxPayAppCart")
    public Map wxPayAppCart(String payNum, BigDecimal payMoney,Long userId) throws Exception {
        return service.doUnifiedOrderCart(payNum,1, payMoney,userId);
    }

    @IgnoreSecurity
    @ApiOperation("公众号JSAPI支付")
    @PostMapping("/wxPayWebCart")
    public Map wxPayWebCart(String payNum,BigDecimal payMoney,Long userId) throws Exception {
        return service.doUnifiedOrderCart(payNum,2, payMoney,userId);
    }

    @IgnoreSecurity
    @ApiOperation("小程序支付")
    @PostMapping("/wxPayMpCart")
    public Map wxPayMpCart(String payNum,BigDecimal payMoney,Long userId) throws Exception {
        return service.doUnifiedOrderCart(payNum,3, payMoney,userId);
    }


    @IgnoreSecurity
    @PostMapping("/notify")
    public String wxPayNotify(HttpServletRequest request) {
        return not(request,1);
    }

    @IgnoreSecurity
    @PostMapping("/notifys")
    public String wxPayNotifys(HttpServletRequest request) {
        return not(request,2);
    }


    @IgnoreSecurity
    @PostMapping("/notifyMp")
    public String notifyMp(HttpServletRequest request) {
        return not(request,3);
    }

    @IgnoreSecurity
    @PostMapping("/notifyCart")
    public String wxPayNotifyCart(HttpServletRequest request) {
        return notCart(request,1);
    }

    @IgnoreSecurity
    @PostMapping("/notifysCart")
    public String wxPayNotifysCart(HttpServletRequest request) {
        return notCart(request,2);
    }


    @IgnoreSecurity
    @PostMapping("/notifyMpCart")
    public String notifyMpCart(HttpServletRequest request) {
        return notCart(request,3);
    }

    private String not(HttpServletRequest request,Integer type){
        String resXml = "";
        try {
            InputStream inputStream = request.getInputStream();
            //将InputStream转换成xmlString
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder sb = new StringBuilder();
            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
            } catch (IOException e) {
                log.info(e.getMessage());
            } finally {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            resXml = sb.toString();
            String result = service.payBack(resXml,type);
            log.info("成功");
            log.info(result);
            return result;
        } catch (Exception e) {
            log.info("微信手机支付失败:" + e.getMessage());
            String result = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>" + "<return_msg><![CDATA[报文为空]]></return_msg>" + "</xml> ";
            log.info("失败");
            log.info(result);
            return result;
        }
    }

    private String notCart(HttpServletRequest request,Integer type){
        String resXml = "";
        try {
            InputStream inputStream = request.getInputStream();
            //将InputStream转换成xmlString
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder sb = new StringBuilder();
            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
            } catch (IOException e) {
                log.info(e.getMessage());
            } finally {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            resXml = sb.toString();
            String result = service.payBackCart(resXml,type);
            log.info("成功");
            log.info(result);
            return result;
        } catch (Exception e) {
            log.info("微信手机支付失败:" + e.getMessage());
            String result = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>" + "<return_msg><![CDATA[报文为空]]></return_msg>" + "</xml> ";
            log.info("失败");
            log.info(result);
            return result;
        }
    }

    @IgnoreSecurity
    @ApiOperation("微信退款")
    @PostMapping("/refund")
    public Result refund(Long ordersId){
        return ResultUtil.success(service.refund(ordersId));
    }

}
